package com.dragon.concurrent.demo.cyclicbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author dragon
 * @version V1.0.0-RELEASE
 * @date 2019/6/6 11:37
 */
public class CyclicBarrierDemo {
    public static class Soldier implements Runnable {
        private String solider;
        private final CyclicBarrier cyclic;

        public Soldier(String solider, CyclicBarrier cyclic) {
            this.solider = solider;
            this.cyclic = cyclic;
        }

        @Override
        public void run() {
            // 等待所有的士兵到齐
            try {
                cyclic.await();
                doWork();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }

        private void doWork() {
            System.out.println(this.solider + "完成工作");
        }
    }
}
